import java.util.*;

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.io.GraphMLFile;


public class VerySimpleSimulation {
	
	/**
	 * 	 * @param args
	 */
	public static void main(String[] args) {
		
		// Creating the local graphFile variable.
		GraphMLFile gml = new GraphMLFile();
		// Loading the file and creating the Graph.
		Graph graph = gml.load("Casestudy2/taskgraph_2.graphml");
		
		//Job queue
		Set<Job> job_queue = new HashSet<Job>();
		
		//LCM -should be calculated but for now will use constant
		int LCM = 60;
		
		// Printing the task details in the loaded task graph 
		Iterator<Object> iterator = graph.getVertices().iterator();
		for (Iterator<Object> iter = iterator; iter.hasNext();) {
			Vertex v = (Vertex) iter.next();	
			//Generate Jobs from the xml file.
			Job newJob = new Job(v.getUserDatum("Name").toString(), getBCETValue(v), getWCETValue(v),getPeriodValue(v),getDeadlineValue(v),getMappedToProc(v),getPriorityValue(v));
			//Adding them to a queue.
			job_queue.add(newJob);
		};

		//Used for generating the random number in the for loop.
		Random rand = new Random();
		
		//This just iterates through the queue and displays the tasks in the queue.
		for( Iterator<Job> iter = job_queue.iterator(); iter.hasNext();){
			Job j = iter.next();
			
			System.out.print(j.print());
			//This generates a random number from the BCET and WCET
			System.out.print(" " + getRandomInteger(j.getBCET(), j.getWCET(), rand));
		}
			
		
		
	}
	
	/**
	 * Returns the WCET Value of the Task Node.
	 * 
	 * It converts the exisiting UserDatum Value to integer
	 * 
	 * @return An Int
	 */
	public static int getWCETValue(Vertex v) {
		return Integer.parseInt((v.getUserDatum("WCET")).toString());
	}

	/**
	 * Returns the Period Value of the Task Node.
	 * 
	 * It converts the exisiting UserDatum Value to integer
	 * 
	 * @return An Int
	 */
	public static int getPeriodValue(Vertex v) {
		return Integer.parseInt((v.getUserDatum("Period")).toString());
	}

	/**
	 * Returns the Deadline Value of the Task Node.
	 * 
	 * It converts the exisiting UserDatum Value to integer
	 * 
	 * @return An Int
	 */
	public static int getDeadlineValue(Vertex v) {
		return Integer.parseInt((v.getUserDatum("Deadline")).toString());
	}
	
	/**
	 * Returns the BCET Value of the Task Node.
	 * 
	 * It converts the exisiting UserDatum Value to integer
	 * 
	 * @return An Int
	 */
	public static int getBCETValue(Vertex v) {
		return Integer.parseInt((v.getUserDatum("BCET")).toString());
	}
	
	/**
	 * Returns the Priority Value of the Task Node.
	 * 
	 * It converts the exisiting UserDatum Value to integer
	 * 
	 * @return An Int
	 */
	public static int getPriorityValue(Vertex v) {
		return Integer.parseInt((v.getUserDatum("Priority")).toString());
	}
	

	

	/**
	 * Returns the Processor Name on which the task is mapped to
	 * 
	 * It converts the exisiting UserDatum Value to integer
	 * 
	 * @return An Int
	 */
	public static String getMappedToProc(Vertex v) {
		
		return (String) v.getUserDatum("MappedTo");
		
	}
	
	/**
	 * Returns a random number generated from a range of numbers. This 
	 * range is defined by a start number A and a end number B.
	 * 
	 * 
	 */
	public static int getRandomInteger(int start, int end, Random rand){
		
		if ( start > end ) {
		      throw new IllegalArgumentException("Start cannot exceed End.");
		}
		
		//get the range, casting to long to avoid overflow problems
	    long range = (long)end - (long)start + 1;
	    // compute a fraction of the range, 0 <= frac < range
		
	    long fraction = (long)(range * rand.nextDouble());
		
	    int randomNumber =  (int)(fraction + start);    
		
	    return randomNumber;
		  
	}
	
}
